package com.ncst.backend.controller;

import com.ncst.backend.common.Result;
import com.ncst.backend.domain.Disease;
import com.ncst.backend.service.DiseaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 疾病管理控制器
 */
@Api(tags = "疾病管理接口")
@RestController
@RequestMapping("/api/diseases")
public class DiseaseController {

    @Autowired
    private DiseaseService diseaseService;

    /**
     * 根据ID查询疾病
     */
    @ApiOperation(value = "根据ID查询疾病", notes = "通过疾病ID获取详细信息")
    @GetMapping("/{id}")
    public Result<Disease> getDiseaseById(
            @ApiParam(value = "疾病ID", required = true, example = "1")
            @PathVariable Integer id) {
        Disease disease = diseaseService.selectById(id);
        if (disease != null) {
            return Result.success(disease);
        }
        return Result.notFound("未找到该疾病信息");
    }

    /**
     * 查询所有疾病
     */
    @ApiOperation(value = "查询所有疾病", notes = "获取系统中所有的疾病信息列表")
    @GetMapping
    public Result<List<Disease>> getAllDiseases() {
        List<Disease> diseases = diseaseService.selectAll();
        return Result.success(diseases);
    }

    /**
     * 条件查询疾病
     */
    @ApiOperation(value = "条件查询疾病", notes = "根据传入的疾病条件查询匹配的疾病列表")
    @PostMapping("/condition")
    public Result<List<Disease>> getDiseasesByCondition(
            @ApiParam(value = "疾病查询条件", required = true)
            @RequestBody Disease disease) {
        List<Disease> diseases = diseaseService.selectByCondition(disease);
        return Result.success(diseases);
    }

    /**
     * 分页查询疾病
     */
    @ApiOperation(value = "分页查询疾病", notes = "按页码和每页大小分页查询疾病列表")
    @GetMapping("/page")
    public Result<Map<String, Object>> getDiseasesByPage(
            @ApiParam(value = "页码，从1开始", required = true, example = "1")
            @RequestParam Integer page,
            @ApiParam(value = "每页条数", required = true, example = "10")
            @RequestParam Integer size) {
        List<Disease> diseases = diseaseService.selectByPage(page, size);
        Long total = diseaseService.countAll();

        Map<String, Object> result = new HashMap<>();
        result.put("data", diseases);
        result.put("total", total);
        result.put("page", page);
        result.put("size", size);
        result.put("pages", (total + size - 1) / size);

        return Result.success(result);
    }

    /**
     * 条件分页查询
     */
    @ApiOperation(value = "条件分页查询疾病", notes = "结合条件和分页查询疾病列表")
    @PostMapping("/page/condition")
    public Result<Map<String, Object>> getDiseasesByConditionAndPage(
            @ApiParam(value = "页码，从1开始", required = true, example = "1")
            @RequestParam Integer page,
            @ApiParam(value = "每页条数", required = true, example = "10")
            @RequestParam Integer size,
            @ApiParam(value = "疾病查询条件", required = true)
            @RequestBody Disease disease) {
        List<Disease> diseases = diseaseService.selectByCondition(disease);
        Long total = diseaseService.countByCondition(disease);

        Map<String, Object> result = new HashMap<>();
        result.put("data", diseases);
        result.put("total", total);
        result.put("page", page);
        result.put("size", size);
        result.put("pages", (total + size - 1) / size);

        return Result.success(result);
    }

    /**
     * 添加疾病
     */
    @ApiOperation(value = "添加疾病", notes = "新增一条疾病信息")
    @PostMapping
    public Result<Integer> addDisease(
            @ApiParam(value = "疾病信息", required = true)
            @RequestBody Disease disease) {
        int result = diseaseService.insert(disease);
        return Result.success("添加成功", result);
    }

    /**
     * 批量添加疾病
     */
    @ApiOperation(value = "批量添加疾病", notes = "一次性添加多条疾病信息")
    @PostMapping("/batch")
    public Result<Integer> addDiseasesBatch(
            @ApiParam(value = "疾病列表", required = true)
            @RequestBody List<Disease> diseases) {
        int result = diseaseService.insertBatch(diseases);
        return Result.success("批量添加成功", result);
    }

    /**
     * 更新疾病
     */
    @ApiOperation(value = "更新疾病", notes = "根据ID更新疾病信息")
    @PutMapping("/{id}")
    public Result<Integer> updateDisease(
            @ApiParam(value = "疾病ID", required = true, example = "1")
            @PathVariable Integer id,
            @ApiParam(value = "更新的疾病信息", required = true)
            @RequestBody Disease disease) {
        disease.setId(id);
        int result = diseaseService.updateById(disease);
        return Result.success("更新成功", result);
    }

    /**
     * 根据ID删除疾病
     */
    @ApiOperation(value = "删除疾病", notes = "根据ID删除一条疾病信息")
    @DeleteMapping("/{id}")
    public Result<Integer> deleteDisease(
            @ApiParam(value = "疾病ID", required = true, example = "1")
            @PathVariable Integer id) {
        int result = diseaseService.deleteById(id);
        return Result.success("删除成功", result);
    }

    /**
     * 批量删除疾病
     */
    @ApiOperation(value = "批量删除疾病", notes = "一次性删除多条疾病信息")
    @DeleteMapping("/batch")
    public Result<Integer> deleteDiseasesBatch(
            @ApiParam(value = "疾病ID列表", required = true)
            @RequestBody List<Integer> ids) {
        int result = diseaseService.deleteByIds(ids);
        return Result.success("批量删除成功", result);
    }
}
